package com.example.project3;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.lang.ref.WeakReference;

public class java4 extends AppCompatActivity {

    // 声明控件
    private ProgressBar progressCircle;
    private Button btnLogin;
    private Button btnRegister;
    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbAgree;

    // 使用静态内部类+弱引用的Handler，避免内存泄漏
    private static class MyHandler extends Handler {
        private final WeakReference<java4> activityRef;

        public MyHandler(java4 activity) {
            super(Looper.getMainLooper());
            activityRef = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(android.os.Message msg) {
            java4 activity = activityRef.get();
            if (activity == null) {
                return;
            }
            // 处理消息
            String type = (String) msg.obj;
            activity.progressCircle.setVisibility(View.GONE);
            activity.btnLogin.setEnabled(true);
            activity.btnRegister.setEnabled(true);
            Toast.makeText(activity, type + "成功", Toast.LENGTH_SHORT).show();
        }
    }

    private MyHandler mainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainHandler = new MyHandler(this);

        // 1. 初始化控件
        initViews();

        // 2. 设置登录按钮点击事件
        btnLogin.setOnClickListener(v -> startLoading("登录"));

        // 3. 设置注册按钮点击事件
        btnRegister.setOnClickListener(v -> startLoading("注册"));
    }

    // 初始化所有控件（增加了输入框和复选框的初始化）
    // 修改initViews()方法中的控件ID引用
    private void initViews() {
        progressCircle = findViewById(R.id.progressBar); // 进度条ID修正
        btnLogin = findViewById(R.id.button_login);      // 登录按钮ID修正
        btnRegister = findViewById(R.id.button_register);// 注册按钮ID修正
        etUsername = findViewById(R.id.editText_username);// 用户名输入框ID修正
        etPassword = findViewById(R.id.editText_password);// 密码输入框ID修正
        cbAgree = findViewById(R.id.cb_agree);           // 复选框ID（布局中需添加）
    }

    // 模拟加载过程（增加输入校验）
    private void startLoading(String type) {
        // 输入校验
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (username.isEmpty()) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (password.isEmpty()) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!cbAgree.isChecked()) {
            Toast.makeText(this, "请同意用户协议", Toast.LENGTH_SHORT).show();
            return;
        }

        // 显示进度条，禁用按钮
        progressCircle.setVisibility(View.VISIBLE);
        btnLogin.setEnabled(false);
        btnRegister.setEnabled(false);
        Toast.makeText(this, "正在" + type + "...", Toast.LENGTH_SHORT).show();

        // 启动子线程，模拟网络请求
        new Thread(() -> {
            try {
                Thread.sleep(3000); // 模拟耗时操作
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt(); // 恢复中断状态
                return;
            }

            // 通过Handler回到主线程更新UI
            mainHandler.obtainMessage(0, type).sendToTarget();
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除所有未处理的消息，避免内存泄漏
        mainHandler.removeCallbacksAndMessages(null);
    }
}